<?php

/**
 * 自动生成 SysUploadService
 * 
 * Apps/Common/Service/SysUploadService.class.php
 * ryadmin @ 2015-07-01
 */
namespace Common\Service;

class SysUploadService {
  private static $instance;
  public static function instance() {
    if (self::$instance == null) {
      $instance = new SysUploadService ();
    }
    return $instance;
  }
  function get_default_row() {
    return array (
        'id' => '',
        'type' => '',
        'file' => '',
        'user' => '',
        'admin' => '',
        'create_date' => date ( 'Y-m-d H:i:s' ),
        'tag' => '',
        'expand' => '' 
    );
  }
  function get_by_id($id) {
    $SysUpload = M ( 'SysUpload' );
    $data = $SysUpload->find ( $id );
    return $data ? $data : array ();
  }
  function get_by_cond($config) {
    $default = array (
        'page' => 1,
        'page_size' => 6,
        'status' => '',
        'count' => FALSE,
        'order' => 'DESC',
        'sort' => 'id' 
    );
    
    $config = extend ( $config, $default );
    
    $SysUpload = M ( 'SysUpload' );
    
    $where = array ();
    
    if (! empty ( $config ['status'] )) {
      $where ['status'] = $config ['status'];
    }
    
    if ($config ['count']) {
      return $SysUpload->where ( $where )
        ->count ();
    } else {
      $order = $config ['sort'] . ' ' . $config ['order'];
      $limit = ($config ['page'] - 1) * $config ['page_size'] . ' , ' . $config ['page_size'];
      $data = $SysUpload->where ( $where )
        ->limit ( $limit )
        ->order ( $order )
        ->select ();
      // echo $SysUpload->_sql();
      return $data ? $data : array ();
    }
  }
  function create($data, $isAjax = true) {
    $SysUpload = M ( 'SysUpload' );
    $ret = $SysUpload->add ( $data );
    if (isAjax) {
      if ($ret) {
        return ajax_arr ( '添加成功', TRUE, array (
            'id' => $ret 
        ) );
      } else {
        return ajax_arr ( '添加失败', FALSE );
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }
  function update($id, $data) {
    $SysUpload = M ( 'SysUpload' );
    
    $ret = $SysUpload->where ( "id = %d", $id )
      ->save ( $data );
    if ($ret) {
      return ajax_arr ( '编辑成功', TRUE );
    } else {
      return ajax_arr ( '编辑失败', FALSE );
    }
  }
  function delete($ids) {
    $SysUpload = M ( 'SysUpload' );
    $ret = $SysUpload->delete ( $ids );
    if ($ret == 0) {
      return ajax_arr ( '未删除任何数据', FALSE );
    } else if (! $ret) {
      return ajax_arr ( '删除失败', FALSE );
    } else {
      return ajax_arr ( '删除' . $ret . '行数据', TRUE );
    }
  }
  function get_upload_img($where) {
    $SysUpload = M ( 'SysUpload' );
    $data = $SysUpload->field ( "file" )
      ->where ( $where )
      ->select ();
    foreach ( $data as &$row ) {
      $row = U ( $row ["file"] );
    }
    return $data;
  }
}
